//
//  LFAdImageViewCell.m
//  QingDaoCommunity
//
//  Created by 李学林 on 15/11/25.
//  Copyright © 2015年 teteTechnology. All rights reserved.
//

#import "LFAdImageViewCell.h"
#import "iCarousel.h"
#import "UIImageView+WebCache.h"
#import "LFFocus.h"


@interface LFAdImageViewCell ()<iCarouselDataSource, iCarouselDelegate>

@property (weak, nonatomic) IBOutlet iCarousel *carousel;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;


@property (nonatomic, assign) BOOL wrap;
@property (nonatomic, strong) NSMutableArray *items;

@property (nonatomic,strong) NSTimer *timer;

@end
@implementation LFAdImageViewCell
- (void)awakeFromNib{

    // 设置初始化数据
    [self setUp];
    //
    self.carousel.type = iCarouselTypeLinear;
    
    [self.carousel reloadData];
    
    
    
    
    
}

#pragma mark ---------------LFHomeModel的set方法--------------
- (void)setHomeModel:(LFHomeModel *)homeModel{

    _homeModel = homeModel;
    
    self.pageControl.numberOfPages = [_homeModel.focus.list count];
 
    [self startTimer];
    
    [_carousel reloadData];
    
    
    
}


- (void)setUp
{

    self.wrap = YES;

    
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(__unused UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}
#pragma mark UIActionSheet methods

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex >= 0)
    {
        //map button index to carousel type
        iCarouselType type = buttonIndex;
        
        //carousel can smoothly animate between types
        [UIView beginAnimations:nil context:nil];
        self.carousel.type = type;
        [UIView commitAnimations];

    }
}

#pragma mark -
#pragma mark iCarousel methods

- (NSInteger)numberOfItemsInCarousel:(__unused iCarousel *)carousel
{
    NSLog(@"========================%td",_homeModel.focus.list.count);

    return (NSInteger)[self.homeModel.focus.list count];
}

- (UIView *)carousel:(__unused iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{

    
    if (view == nil)
    {
        
        //获取实例
        LFFocus *fouce = _homeModel.focus.list[index];
        
        view = [[UIImageView alloc] initWithFrame:self.bounds];
//                ((UIImageView *)view).image = [UIImage imageNamed:@"123.jpg"];
        
        // 设置图像
        [((UIImageView *)view) sd_setImageWithURL:[NSURL URLWithString:fouce.cover]];
        
        self.titleLabel.text = fouce.title;
        
        // 设置当前的分页
        self.pageControl.currentPage = index;
       

    }

    return view;
}

- (NSInteger)numberOfPlaceholdersInCarousel:(__unused iCarousel *)carousel
{
    //note: placeholder views are only displayed on some carousels if wrapping is disabled
    return 2;
}

- (UIView *)carousel:(__unused iCarousel *)carousel placeholderViewAtIndex:(NSInteger)index reusingView:(UIView *)view
{

    
    if (view == nil)
    {
        
        //获取实例
        LFFocus *fouce = _homeModel.focus.list[index];
        
        view = [[UIImageView alloc] initWithFrame:self.bounds];
//        ((UIImageView *)view).image = [UIImage imageNamed:@"123.jpg"];

        // 设置图像
        [((UIImageView *)view) sd_setImageWithURL:[NSURL URLWithString:fouce.cover]];
        
        self.titleLabel.text = fouce.title;
        

    }


    
    return view;
}

- (CATransform3D)carousel:(__unused iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
{
    //implement 'flip3D' style carousel
    transform = CATransform3DRotate(transform, M_PI / 8.0f, 0.0f, 1.0f, 0.0f);
    return CATransform3DTranslate(transform, 0.0f, 0.0f, offset * self.carousel.itemWidth);
}

- (CGFloat)carousel:(__unused iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    //customize carousel display
    switch (option)
    {
        case iCarouselOptionWrap:
        {
            //normally you would hard-code this to YES or NO
            return self.wrap;
        }
        case iCarouselOptionSpacing:
        {
            //add a bit of spacing between the item views
            return value * 1.0f;
        }
        case iCarouselOptionFadeMax:
        {
            if (self.carousel.type == iCarouselTypeCustom)
            {
                //set opacity based on distance from camera
                return 0.0f;
            }
            return value;
        }
        case iCarouselOptionShowBackfaces:
        case iCarouselOptionRadius:
        case iCarouselOptionAngle:
        case iCarouselOptionArc:
        case iCarouselOptionTilt:
        case iCarouselOptionCount:
        case iCarouselOptionFadeMin:
        case iCarouselOptionFadeMinAlpha:
        case iCarouselOptionFadeRange:
        case iCarouselOptionOffsetMultiplier:
        case iCarouselOptionVisibleItems:
        {
            return value;
        }
    }
}

#pragma mark -
#pragma mark iCarousel taps

- (void)carousel:(__unused iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    NSNumber *item = (self.homeModel.focus.list)[(NSUInteger)index];
    NSLog(@"Tapped view number: %@", item);
}


- (void)carouselCurrentItemIndexDidChange:(__unused iCarousel *)carousel
{
    NSLog(@"Index: %@", @(self.carousel.currentItemIndex));
    //获取实例
    LFFocus *fouce = _homeModel.focus.list[self.carousel.currentItemIndex];

    self.titleLabel.text = fouce.title;
    
    // 设置当前的分页
    self.pageControl.currentPage = self.carousel.currentItemIndex;
}
-(void)startTimer{
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(pageChange) userInfo:nil repeats:YES];
    
    //    [_timer fire];
}

-(void)pageChange{
    
    NSInteger currentPage = _pageControl.currentPage;
    
    //    CGFloat offset = self.carousel.scrollOffset;
    
    if (currentPage >= self.pageControl.numberOfPages - 1) {
        
        currentPage = 0;
        
    }else{
        
        currentPage++;
        
    }
    _pageControl.currentPage = currentPage;
    
    [self.carousel scrollToItemAtIndex:_pageControl.currentPage animated:YES];
}

#pragma mark ----------iCarouselDelegate--------------

- (void)carouselWillBeginDragging:(iCarousel *)carousel{
    
    [self.timer invalidate];
}
- (void)carouselDidEndDragging:(iCarousel *)carousel willDecelerate:(BOOL)decelerat{
    
    [self startTimer];
}


@end

